package com.test.test01.advice;

import com.test.test01.common.base.error.CommonException;
import com.test.test01.common.base.error.ErrorEnum;
import com.test.test01.common.base.response.BaseResponse;
import com.test.test01.common.base.result.BaseResponseStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * Description: 全局异常处理处理器
 * Create DateTime: 2020-04-17 19:20
 *
 * @author houyi
 */
@Slf4j
@RestControllerAdvice
public class GlobalDefaultExceptionHandler {

    /**
     * 拦截自定义错误信息
     *
     * @param exception CommonException
     * @return 错误信息
     */
    @ExceptionHandler(value = CommonException.class)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse handlerNoHandlerFoundException(CommonException exception) {
        BaseResponse baseResponse = getBaseResponse();
        baseResponse.setMessage(exception.getDetail());
        return baseResponse;
    }

    /**
     * 拦截NoHandlerFoundException
     *
     * @param exception NoHandlerFoundException
     * @return 错误信息
     */
    @ExceptionHandler(value = NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public BaseResponse handlerNoHandlerFoundException(NoHandlerFoundException exception) {
        BaseResponse baseResponse = getBaseResponse();
        baseResponse.setMessage(ErrorEnum.NOT_FOUND.getErrorDescription());
        return baseResponse;
    }

    /**
     * 拦截IllegalArgumentException
     *
     * @param exception IllegalArgumentException
     * @return 错误信息
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public BaseResponse handlerIllegalArgumentException(IllegalArgumentException exception) {
        BaseResponse baseResponse = getBaseResponse();
        baseResponse.setMessage(ErrorEnum.ARG_ERROR.getErrorDescription());
        return baseResponse;
    }

    /**
     * 拦截HttpRequestMethodNotSupportedException
     *
     * @param exception HttpRequestMethodNotSupportedException
     * @return 错误信息
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public BaseResponse handlerHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        BaseResponse baseResponse = getBaseResponse();
        baseResponse.setMessage(ErrorEnum.NOT_SUPPORT_REQUEST_METHOD.getErrorDescription());
        return baseResponse;
    }

    /**
     * 拦截Exception
     *
     * @param exception Exception
     * @return 错误信息
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse handlerException(Exception exception) {
        BaseResponse baseResponse = getBaseResponse();

        if (exception instanceof MethodArgumentNotValidException) {
            baseResponse.setMessage(((MethodArgumentNotValidException) exception).getBindingResult().getFieldError().getDefaultMessage());
        } else {
            baseResponse.setMessage(ErrorEnum.UN_KNOWN.getErrorDescription());
        }
        return baseResponse;
    }


    protected BaseResponse getBaseResponse() {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(BaseResponseStatus.DEFAULT_FAIL_CODE);
        baseResponse.setStatus(BaseResponseStatus.DEFAULT_FAIL);
        return baseResponse;
    }

}
